package com.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.domain.Issue;
import com.domain.Label;
import com.domain.LabelWithIssue;
import com.domainVO.IssueUserLabeCommentlVO;
import com.domainVO.PageInfoVO;
import com.mapper.helping.LabelMapper;
import com.mapper.helping.LabelWithIssueMapper;
import com.mapper.living.UserDao;
import com.service.ILabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.util.HotUtil;
import com.util.UserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大忽悠
 * @since 2022年02月08日
 */
@RequiredArgsConstructor
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements ILabelService {
   private final LabelMapper labelMapper;
   private final UserDao userDao;
   private final LabelWithIssueMapper labelWithIssueMapper;
   private final HotUtil hotUtil;
   private final IssueServiceImpl issueService;


   @Override
    public Map<String,Object> getIssuesUnderTargetLabel(Integer label_id, Integer startPage, Integer pageSize) {
        Page<Issue> page=new Page<>(startPage,pageSize);
        List<Issue> issues = labelMapper.getIssuesUnderTargetLabel(label_id,page);
        //key:问题id ,value:返回给前端的数据
        List<IssueUserLabeCommentlVO> res=new ArrayList<>();
        //通用查询处理
        issueService.commonSearchOpeartion(issues,res,true);
        //对应标签热度增加
       Label label = labelMapper.selectById(label_id);
       label.setHot(label.getHot()+hotUtil.getLABEL_LIKE_HOT_ADD());
       return PageInfoVO.builder().autoWired(page,res);
    }

    @Override
    public Map<String, Object> vagueSearch(String content, Integer startPage, Integer pageSize) {
        Page<Label> page=new Page<>(startPage,pageSize);
        Page<Label> labelPage = labelMapper.selectPage(page, new QueryWrapper<Label>().like("content", content)
                .orderByDesc("id"));
        //被搜索出来的标签热度增加
        List<Label> labels = labelPage.getRecords();
        labels.forEach(x->{
            x.setHot(x.getHot()+hotUtil.getLABEL_LIKE_HOT_ADD());
            labelMapper.updateById(x);
        });
        return PageInfoVO.builder().autoWired(page,labelPage.getRecords());
    }

    /**
     * <p>
     *     1.删除当前标签和问题关联的映射
     *     2.删除标签本身
     * </p>
     * @param label_id
     * @return
     */
    @Transactional(value = "primaryTransactionManager")
    @Override
    public Boolean delTargetLabel(Integer label_id) {
        labelWithIssueMapper.delete(new QueryWrapper<LabelWithIssue>().eq("label_id", label_id));
        labelMapper.deleteById(label_id);
        return true;
    }

    /**
     * <P>
     *     获取未审核的标签
     * </P>
     * @param startPage
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> getUnAuditLabels(Integer startPage, Integer pageSize) {
        Page<Label> page=new Page<>(startPage,pageSize);
        List<Label> labels = labelMapper.selectPage(page, new QueryWrapper<Label>()
                .eq("is_audited", 0).orderByDesc("id")).getRecords();
        return PageInfoVO.builder().autoWired(page,labels);
    }

    @Override
    public Boolean passLabel(Integer label_id) {
        return labelMapper.passLabel(label_id);
    }

}
